package com.pzf.publish.service;

import com.pzf.publish.config.RedisStreamConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.connection.stream.StringRecord;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * @Author: AdobePeng
 * @Date: 2021/3/9 10:41
 */
@Service
public class PublishService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisStreamConfig redisStreamConfig;

    public void test(String msg){
            // 创建消息记录, 以及指定stream
            StringRecord stringRecord = StreamRecords.string(Collections.singletonMap("name", msg)).withStreamKey(redisStreamConfig.getStream());
            // 将消息添加至消息队列中
            this.stringRedisTemplate.opsForStream().add(stringRecord);
            // 发往流mystream2
            sendToStream2(msg);
    }

    private void sendToStream2(String msg){
        // 创建消息记录, 以及指定stream
        StringRecord stringRecord = StreamRecords.string(Collections.singletonMap("name", msg)).withStreamKey(redisStreamConfig.getStream2());
        // 将消息添加至消息队列中
        this.stringRedisTemplate.opsForStream().add(stringRecord);
    }
}
